/*
战斗技能选择框
*/
window._SKILLSELECTMANAGER = {

    _ARR:null,

    _SELECTTYPE:null,//skill--技能  item--道具

    _INDEX:0,

    _PAGESIZE:4,

    _selectLabel:null,


    getCurrentPlayer:function(){
        return _CHESSFIGHT.currentPlayer;
    },


    showSelect:function(parentNode,arr,type){

        _SKILLSELECTMANAGER._ARR=arr;

        _SKILLSELECTMANAGER._INDEX=0;

        _SKILLSELECTMANAGER._SELECTTYPE=type;

        var choiceObj = _GRM.getNodeObj('skillSelectLabel');
		var selectLabel = choiceObj.node;
        _SKILLSELECTMANAGER._selectLabel = selectLabel;

        if(choiceObj.isnew){

            selectLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
                _GRM.putNode(this);
            }, selectLabel);

            selectLabel.getChildByName('background')
                       .getChildByName('page')
                       .getChildByName('btnRight')
                       .on(cc.Node.EventType.TOUCH_START, function(){
                            _SKILLSELECTMANAGER.refreshPage(1);
                        }, selectLabel);
            
            selectLabel.getChildByName('background')
                       .getChildByName('page')
                       .getChildByName('btnLeft')
                       .on(cc.Node.EventType.TOUCH_START, function(){
                            _SKILLSELECTMANAGER.refreshPage(-1);
                        }, selectLabel);
            
            var skillbtns = selectLabel.getChildByName('skills').getChildren();
            for(var i=0;i<skillbtns.length;i++){
                skillbtns[i].index = i;
                skillbtns[i].on(cc.Node.EventType.TOUCH_START,_SKILLSELECTMANAGER.skillClick,skillbtns[i]);
            }
                            

        }

        parentNode.addChild(selectLabel);

        _SKILLSELECTMANAGER.refreshLabel(selectLabel);

    },

    refreshPage:function(dir){

        //console.log(dir);

        if(
            _SKILLSELECTMANAGER._INDEX+dir<0
            ||(_SKILLSELECTMANAGER._INDEX+dir)*_SKILLSELECTMANAGER._PAGESIZE>=_SKILLSELECTMANAGER._ARR.length
            )
            return ;

        _SKILLSELECTMANAGER._INDEX+=dir
        _SKILLSELECTMANAGER.refreshLabel( _SKILLSELECTMANAGER._selectLabel);
    },

    refreshLabel:function(selectLabel){
        var skillbtns = selectLabel.getChildByName('skills').getChildren();
        var start = _SKILLSELECTMANAGER._INDEX*_SKILLSELECTMANAGER._PAGESIZE;
        //var end = start+_SKILLSELECTMANAGER._PAGESIZE;
        for(var i=0;i<_SKILLSELECTMANAGER._PAGESIZE;i++){


            var skillname = _SKILLSELECTMANAGER._ARR?_SKILLSELECTMANAGER._ARR[start+i]:null;
            if(skillname){
                skillbtns[i].active = true;

                if(_SKILLSELECTMANAGER._SELECTTYPE=='skill'){
                    if(_SKILL[skillname]){

                        if(!_MFD.isMagicEnough(_SKILLSELECTMANAGER.getCurrentPlayer(),skillname)){
                            skillbtns[i].getChildByName('title').getComponent(cc.RichText).string = skillname+"<color=red>[魔法不足]</color>";
                        }else{
                            skillbtns[i].getChildByName('title').getComponent(cc.RichText).string = skillname;
                        }

                        
                        skillbtns[i].getChildByName('content').getComponent(cc.Label).string = _SKILL[skillname].des;
                    }else{
                        skillbtns[i].getChildByName('title').getComponent(cc.RichText).string = skillname;
                        skillbtns[i].getChildByName('content').getComponent(cc.Label).string = '该技能无法使用';
                    }
                }else{
                    if(
                        /*skillname.type
                        &&_FIGHTITEM[skillname.type]
                        &&(_FIGHTITEM[skillname.type].skilltype=='attack'
                            ||_FIGHTITEM[skillname.type].skilltype=='buff'
                            ||_FIGHTITEM[skillname.type].skilltype=='debuff'
                            )
                        */
                        _FIGHTITEMHELPER.canUseInFight(skillname.type)
                    ){
                        skillbtns[i].getChildByName('title').getComponent(cc.RichText).string = skillname.type+"["+(skillname.nmb?skillname.nmb:1)+"]";
                        //skillbtns[i].getChildByName('content').getComponent(cc.Label).string = _FIGHTITEM[skillname.type].des;
                        skillbtns[i].getChildByName('content').getComponent(cc.Label).string = _IOF.getGoodsDetailStr(skillname);
                    }else{
                        skillbtns[i].getChildByName('title').getComponent(cc.RichText).string = skillname.type;
                        skillbtns[i].getChildByName('content').getComponent(cc.Label).string = '该道具无法使用';
                    }
                }

            }else{
                skillbtns[i].active = false;
            }
        }
    },

    skillClick:function(){
        var realindex = _SKILLSELECTMANAGER._INDEX*_SKILLSELECTMANAGER._PAGESIZE+this.index;
        var skillname = _SKILLSELECTMANAGER._ARR[realindex];
        
        _GRM.putNode(_SKILLSELECTMANAGER._selectLabel);

        console.log('道具&技能选择：',skillname);

        if(_SKILL[skillname]&&_SKILL[skillname].single){

            if(!_MFD.isMagicEnough(_SKILLSELECTMANAGER.getCurrentPlayer(),skillname)){
                _TIPS.showMessage('技能灯不足无法使用');
                return;
            }

            _CHESSFIGHT.currentCommond = _MCF._ATYPE_SKILL;
            _CHESSFIGHT.currentSkill = skillname;
            _MCFW.getCurrentPlayerSkillFanwei(skillname);

        }else if(_SKILL[skillname]&&!_SKILL[skillname].single){

            if(!_MFD.isMagicEnough(_SKILLSELECTMANAGER.getCurrentPlayer(),skillname)){
                _TIPS.showMessage('技能灯不足无法使用');
                return;
            }

            _CHESSFIGHT.currentCommond = _MCF._ATYPE_SKILL;
            _CHESSFIGHT.currentSkill = skillname;
            _CHESSFIGHT.skillActionByPos(_CHESSFIGHT.targetX,_CHESSFIGHT.targetY,_CHESSFIGHT.currentSkill);

        }else if(skillname.objtype=="fightitem"){//道具

            if(
                _FIGHTITEMHELPER.canUseInFight(skillname.type)
            ){

                var itemType = skillname.type;
                _CHESSFIGHT.currentCommond = _MCF._ATYPE_ITEM;
                _CHESSFIGHT.currentItem = itemType;
                _MCFW.getCurrentPlayerItemFanwei(itemType);

            }else{
                _TIPS.showMessage('此道具无法使用');
            }

        }

        
    }


}